﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Infragistics.Windows.DataPresenter;

namespace QLKS.Templates
{
    static public class DataGrid
    {
        static private void TableDeleteColumn(ref DataTable tb, DataColumn cl)
        {
            foreach(DataColumn c in tb.Columns)
                if (c.ColumnName.Equals(cl.ColumnName))
                {
                    tb.Columns.Remove(c);
                    break;
                }
        }

        static private bool CheckColumn(string columns, string cl)
        {
            foreach (string s in columns.Split(','))
                if (s.Equals(cl))
                    return true;
            return false;
        }

        static public bool CheckOption(string StringField,string option)
        {
            string[] fields = StringField.Split(':');
            foreach (string f in fields)
                if (f.ToUpper().Equals(option))
                    return true;
            return false;

        }

        //định dạng field
        static public void FieldSetting(ref XamDataGrid xdg, string s)
        {
            try
            {
                if (xdg == null)
                    return;
                if (string.IsNullOrEmpty(s))
                    return;
                string[] fields = s.Split(';');
                string select = string.Empty;
                FieldLayout Row = new FieldLayout();
                foreach (string fs in fields)
                {
                    string[] f = fs.Split(':');
                    Field fi = new Field(f[0], f[1]);
                    fi.ToolTip = f[0];
                    if (f[2].Trim() != "0")
                        fi.Width = new FieldLength(double.Parse(f[2]));
                    if (CheckOption(fs, "F"))
                        fi.FixedLocation = FixedFieldLocation.FixedToNearEdge;
                    if (CheckOption(fs, "IV"))
                        fi.Visibility = System.Windows.Visibility.Collapsed;
                    Row.Fields.Add(fi);
                }
                xdg.FieldLayouts.Add(Row);
            }
            catch
            {
                return;
            }

        }
        /// <summary>
        /// //////////////////
        /// </summary>
        /// <param name="xdg"></param>
        /// <param name="tb"></param>
        /// <param name="s"></param>
        static public void FieldSetting(ref XamDataGrid  xdg,DataTable tb,string s)
        {
            if (string.IsNullOrEmpty(s.Trim()))
            {
                if (tb != null)
                    xdg.DataSource = tb.DefaultView;
                else
                    xdg.DataSource = null;
                return;
            }
            try
            {
                string column = ",";
                string[] fields = s.Split(';');
                string select = string.Empty;
                FieldLayout Row = new FieldLayout();
                foreach (string fs in fields)
                {
                    string[] f = fs.Split(':');
                    Field fi = new Field(f[0], f[1]);
                    fi.ToolTip = f[0];
                    if(f[2].Trim() != "0")
                        fi.Width = new FieldLength(double.Parse(f[2]));
                    if (CheckOption(fs,"F"))
                        fi.FixedLocation = FixedFieldLocation.FixedToNearEdge;
                    if (CheckOption(fs, "IV"))
                        fi.Visibility = System.Windows.Visibility.Collapsed;
                    Row.Fields.Add(fi);
                    column += f[0] + ",";
                }
                DataTable _tb = tb.Copy();
                foreach (DataColumn cl in tb.Columns)
                    if (!CheckColumn(column, cl.ColumnName))
                        TableDeleteColumn(ref _tb, cl);
                xdg.FieldLayouts.Add(Row);
                xdg.DataSource = _tb.DefaultView;
            }
            catch
            {
                if(tb != null)
                    xdg.DataSource = tb.DefaultView;
                return;
            }
            
        }
    }
}
